import 'dart:io';

import 'package:dio/dio.dart';
import 'package:dio/io.dart';
import 'package:http_proxy/http_proxy.dart';

class YouthProxy {
  /// 设置抓包
  static Future setProxy(Dio? dio) async {
    // if (inProduction) return;

    HttpProxy? proxy = await HttpProxy.createHttpProxy();
    if (proxy.host == null) {
      return;
    }

    dio?.httpClientAdapter = IOHttpClientAdapter(
      createHttpClient: () {
        final HttpClient client = HttpClient();
        client.findProxy = (uri) {
          return "PROXY ${proxy.host}:${proxy.port}";
        };
        // 代理工具会提供一个抓包的自签名证书，会通不过证书校验，所以我们禁用证书校验
        client.badCertificateCallback =
            (X509Certificate cert, String host, int port) => true;
        return client;
      },
    );
  }
}
